package game.Sokoban;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

public class GLScene {

	Context context;
	GL10 gl;
	
	public void init(GL10 gl, Context context){
		this.context=context;
		this.gl=gl;

/*
		gl.glShadeModel(GL10.GL_SMOOTH); 			//Enable Smooth Shading
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); 	//Black Background
		gl.glClearDepthf(1.0f); 					//Depth Buffer Setup
		gl.glEnable(GL10.GL_DEPTH_TEST); 			//Enables Depth Testing
		gl.glDepthFunc(GL10.GL_LEQUAL); 			//The Type Of Depth Testing To Do
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); //Really Nice Perspective Calculations 
*/
	}
	
	
	public void add(GLTexture glTexture) {
		textures.put(glTexture.textureNameId, glTexture);
	}

	public void add(GLSprite glSprite) {
		sprites.add(glSprite);
	}

	public void clear() {
		sprites.clear();
	}

	HashMap<String, GLTexture> textures = new HashMap<String, GLTexture>();
	List <GLSprite> sprites = new ArrayList<GLSprite>();

	public float angle=0;
	
	public void draw(){
		FloatBuffer vertexBuffer;	// buffer holding the vertices
		FloatBuffer textureBuffer;	// buffer holding the texture coordinates

		float verticesCoords[] = {
				-1.0f, -1.0f,  0.0f,		// V1 - bottom left
				-1.0f,  1.0f,  0.0f,		// V2 - top left
				 1.0f, -1.0f,  0.0f,		// V3 - bottom right
				 1.0f,  1.0f,  0.0f			// V4 - top right
		};
		float textureCoords[] = {    		
				// Mapping coordinates for the vertices
				0.0f, 1.0f,		// top left		(V2)
				0.0f, 0.0f,		// bottom left	(V1)
				1.0f, 1.0f,		// top right	(V4)
				1.0f, 0.0f		// bottom right	(V3)
		};

		Iterator<GLSprite> i=sprites.iterator();
		while(i.hasNext()){
			GLSprite sprite=i.next();
			GLTexture texture=textures.get(sprite.textureNameId);

			//now drawing in OpenGL sprite with texture
			verticesCoords[0]=sprite.x-sprite.wigth/2;
			verticesCoords[1]=sprite.y-sprite.height/2;

			verticesCoords[3]=sprite.x-sprite.wigth/2;
			verticesCoords[4]=sprite.y+sprite.height/2;
			
			verticesCoords[6]=sprite.x+sprite.wigth/2;
			verticesCoords[7]=sprite.y-sprite.height/2;
			
			verticesCoords[9]=sprite.x+sprite.wigth/2;
			verticesCoords[10]=sprite.y+sprite.height/2;
			
			
			// a float has 4 bytes so we allocate for each coordinate 4 bytes
			ByteBuffer byteBuffer = ByteBuffer.allocateDirect(verticesCoords.length * 4);
			byteBuffer.order(ByteOrder.nativeOrder());
			
			// allocates the memory from the byte buffer
			vertexBuffer = byteBuffer.asFloatBuffer();
			
			// fill the vertexBuffer with the vertices
			vertexBuffer.put(verticesCoords);
			
			
			// set the cursor position to the beginning of the buffer
			vertexBuffer.position(0);
			
			byteBuffer = ByteBuffer.allocateDirect(textureCoords.length * 4);
			byteBuffer.order(ByteOrder.nativeOrder());
			textureBuffer = byteBuffer.asFloatBuffer();
			textureBuffer.put(textureCoords);
			textureBuffer.position(0);

			
			gl.glBindTexture(GL10.GL_TEXTURE_2D, texture.id);
			
			// Point to our buffers
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			
	
			// Set the face rotation
			gl.glFrontFace(GL10.GL_CW);
			
			// Point to our vertex buffer
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
			
			// Draw the vertices as triangle strip
			gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, verticesCoords.length / 3);

			//Disable the client state before leaving
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		}
	}


	public void setCameraXY(float cameraX, float cameraY) {
		gl.glTranslatef(cameraX, cameraY, -15.0f);		// move camera 5 units INTO the screen
	}
}
